//
//  BaseVC.m
//  MarryBackEnd
//
//  Created by KH_MoSi on 2020/7/29.
//  Copyright © 2020 WPP. All rights reserved.
//

#import "BaseVC.h"

@interface BaseVC ()

@end

@implementation BaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createBaseUI];
}
- (void)createBaseUI
{
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.stausBarStyle = UIStatusBarStyleDefault;
}
- (UIView *)createNaviBarWithTitle:(NSString *)title backButtonBlock:(void (^)(void))backButtonBlock
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, kTopHeight)];
    view.backgroundColor = WhiteColor;
    [self.view addSubview:view];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"public_back_black"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, kStatusBarHeight, 20 + 32, kNavBarHeight);//20x20
    CGFloat top = (kNavBarHeight - 20) * 0.5;
    backButton.imageEdgeInsets = UIEdgeInsetsMake(top, 16, top, 16);
    [backButton addTarget:self action:@selector(clickNaviBarBackButton:) forControlEvents:UIControlEventTouchUpInside];
    self.backBlock = backButtonBlock;
    
    UILabel *titleLab = [[UILabel alloc]init];
    [view addSubview:titleLab];
    titleLab.font = kFontBold(18);
    titleLab.textColor = kColor51;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.text = title;
    titleLab.frame = CGRectMake(50, kStatusBarHeight, WIDTH - 100, 19);
    titleLab.centerY = backButton.centerY;
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, view.height - kLineHeight, view.width, kLineHeight)];
    [view addSubview:lineView];
    lineView.backgroundColor = kLineColor;
    
    self.naviBarView = view;
    self.naviBarTitleLab = titleLab;
    self.naviBarBackButton = backButton;
    self.naviBarBottomLineView = lineView;
    return view;
}
- (UIButton *)addNaviBarRightButtonWithImageName:(NSString *)imageName backRightButtonBlock:(void (^)(void))rightButtonBlock
{
    if (!self.naviBarView) {
        return [UIButton buttonWithType:UIButtonTypeCustom];
    }
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.naviBarView addSubview:backButton];
    [backButton setImage:[UIImage getPNGimageInBundleWithName:imageName] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(WIDTH - 20 - 32, kStatusBarHeight, 20 + 32, kNavBarHeight);//20x20
    CGFloat top = (kNavBarHeight - 20) * 0.5;
    backButton.imageEdgeInsets = UIEdgeInsetsMake(top, 16, top, 16);
    [backButton addTarget:self action:@selector(clickNaviBarRightButton:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButtonBlock = rightButtonBlock;
    return backButton;
}
- (UIButton *)addNaviBarRightButtonWithTitle:(NSString *)title withTitleColor:(UIColor *)titleColor backRightButtonBlock:(void (^)(void))rightButtonBlock
{
    if (!self.naviBarView) {
        return [UIButton buttonWithType:UIButtonTypeCustom];
    }
    CGFloat itemWidth = [title sizeWithFont:kFontRegular(14)].width;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.naviBarView addSubview:backButton];
    [backButton setTitle:title forState:UIControlStateNormal];
    [backButton setTitleColor:titleColor forState:UIControlStateNormal];
    backButton.titleLabel.font = kFontRegular(14);
    backButton.frame = CGRectMake(WIDTH - itemWidth - 32, kStatusBarHeight, itemWidth + 32, kNavBarHeight);
    CGFloat top = (kNavBarHeight - 15) * 0.5;
    backButton.titleEdgeInsets = UIEdgeInsetsMake(top, 16, top, 16);
    [backButton addTarget:self action:@selector(clickNaviBarRightButton:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButtonBlock = rightButtonBlock;
    return backButton;
}

- (void)clickNaviBarBackButton:sender
{
    if (self.backBlock) {
        !self.backBlock ?: self.backBlock ();
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)clickNaviBarRightButton:sender
{
    if (self.rightButtonBlock) {
        !self.rightButtonBlock ?: self.rightButtonBlock ();
    }
}
#pragma mark - 屏幕旋转控制
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    //setNeedsStatusBarAppearanceUpdate
    //return UIStatusBarStyleDefault;
    //return self.stausBarStyle;
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        // Fallback on earlier versions
        return UIStatusBarStyleDefault;
    }
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end

